package com.rise.gpsapp;

import com.rise.externalservice.IGPS;
import com.rise.externalservice.IMap;
import com.rise.externalservice.IServerMap;

public class ConsumerThread extends Thread {

	private IGPS serviceGPS;
	private IMap serviceMap;
	private IServerMap ism;
	private boolean isActive = true;

	public ConsumerThread(IGPS newService, IMap newMapService) {
		this.serviceGPS = newService;
		this.serviceMap = newMapService;
		this.ism = null;
	}

	@Override
	public void run() {
		while (isActive) {
			
			if(this.ism != null){
				System.out.println("Localidade: "
						+ this.ism.getMap(this.serviceGPS.getCoordinate()));
			} else if(this.serviceMap != null){
				System.out.println("Localidade: "
						+ this.serviceMap.getMap(this.serviceGPS.getCoordinate()));
			}
			System.out.println("Coordenadas: "
					+ this.serviceGPS.getCoordinate().toString());
			
			try {
				Thread.sleep(20000);
			} catch (InterruptedException e) {
			}
		}
		this.serviceGPS = null;
	}

	public void stopThread() {
		this.isActive = false;
	}
	
	public void consumindo(){
		if(this.ism != null){
			System.out.println("Localidade: "
					+ this.ism.getMap(this.serviceGPS.getCoordinate()));
		} else if(this.serviceMap != null && this.ism == null){
			System.out.println("Localidade: "
					+ this.serviceMap.getMap(this.serviceGPS.getCoordinate()));
		}
		System.out.println("Coordenadas: "
				+ this.serviceGPS.getCoordinate().toString());
	}
	
	public void setGPSService(IGPS service) {
		this.serviceGPS = service;
	}
	
	public void setIMapService(IMap service) {
		this.serviceMap = service;
	}
	
	public void setMapServer(IServerMap service){
		this.ism = service;
	}

}
